package com.efast.cafe.portal.controller.doc;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.doc.DocGroupQueryBean;
import com.efast.cafe.portal.entity.doc.DocGroup;
import com.efast.cafe.portal.service.doc.IDocGroupService;
@Controller
@RequestMapping("/doc/group")
public class DocGroupController extends  AbstractController{

	private IDocGroupService docGroupService;
	@Autowired
	public void setDocGroupService(IDocGroupService docGroupService) {
		this.docGroupService = docGroupService;
	}



	@RequestMapping(value="/list")
	public @ResponseBody ResultBean queryGroupList(@ModelAttribute DocGroupQueryBean queryBean){
		ResultBean result = new ResultBean(true,null);
		try {
			result.setObject(docGroupService.queryGroupList(queryBean));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
	
	@RequestMapping(value="/add")
	public @ResponseBody ResultBean addGroupList(@RequestBody DocGroup docGroup){
		ResultBean result = new ResultBean(true,null);
		try {
			docGroupService.addGroup(docGroup);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
	
	@RequestMapping(value="/update")
	public @ResponseBody ResultBean updateGroupList(@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true,null);
		String id=null;
		String groupName=null;
		String groupDesc=null;
		if(map.containsKey("id")){
			id=map.get("id");
		}
		if(map.containsKey("groupName")){
			groupName=map.get("groupName");
		}
		if(map.containsKey("groupDesc")){
			groupDesc=map.get("groupDesc");
		}
		if(StringUtils.isBlank(id)){
			throw new ServiceException(ResultBean.ERROR_CODE_PARAMETER);
		}
		DocGroup docGroup=new DocGroup();
		if(id.split("_").length<2){
			throw new ServiceException(ResultBean.ERROR_CODE_PARAMETER);
		}
		docGroup.setId(Integer.parseInt(id.split("_")[1]));
		docGroup.setGroupName(groupName);
		docGroup.setGroupDesc(groupDesc);
		try {
			docGroupService.updateGroup(docGroup);;
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
	
	
	@RequestMapping(value="/delete")
	public @ResponseBody ResultBean deleteGroup(@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true,null);
		String pid=null;
		if(map.containsKey("id")){
			pid=map.get("id");
		}
		if(StringUtils.isBlank(pid)){
			throw new ServiceException(ResultBean.ERROR_CODE_PARAMETER);
		}
		if(!pid.startsWith("group") && !pid.startsWith("folder")){
			//只能在group和folder上创建文件夹
			result.setSuccess(false);
			result.setErrorCode("0001");//
			result.setMessage("只删除工作组或文件夹");
			return result;
		}
		try {
			docGroupService.deleteGroup(pid);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
	
}
